package org.popupcommunication.model;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.PostPersist;
import javax.persistence.PrePersist;
import javax.persistence.Table;
import javax.persistence.Transient;


@Entity
@Table(name = "Groupes")
public class Group implements Comparable<Group>, Serializable{

	private static final long serialVersionUID = 1L;
	transient @Transient
	boolean isBeingPersisted = false;	
	
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private int idGroup;

	@Column(nullable = false, length = 100)
	private String name;	

	@Column
	private String slug;
	
	public String getSlug() {
		return slug;
	}

	public void setSlug(String slug) {
		this.slug = slug;
	}


	@Basic(fetch=FetchType.EAGER)	
	@ManyToMany(cascade=CascadeType.ALL,mappedBy ="listGroup")
	private Set<User> listUser = new HashSet<User>();

	@OneToMany(mappedBy = "forum", cascade={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH})
	private Set<SujetForum> listSujets;		

	public Set<SujetForum> getSujets() {
		return listSujets;
	}

	public void setSujets(Set<SujetForum> sujets) {
		this.listSujets = sujets;
	}
	
	public void addSujet(SujetForum s) {
		this.listSujets.add(s);
	}
	
	public void addUser(User u){
		this.listUser.add(u);
	}
		
	public Set<User> getListUser() {
		return listUser;
	}
	
	public void setListUser(Set<User> listUser) {
		this.listUser = listUser;
	}
	public Group(){}
	public Group(String name){
		this.name=name;
	}

	public int getIdGroup() {
		return idGroup;
	}

	public void setIdGroup(int idGroup) {
		this.idGroup = idGroup;
	}

	public String getName() {
		return name;
	}

	public void setName(String groupName) {
		this.name = groupName;
	}

	@Override
	public int compareTo(Group gu) {
			return name.compareTo(gu.getName());
	}

	@Override
	public int hashCode() {
		if (getName() != null) {
			return this.getName().hashCode();
		} else if (isBeingPersisted) {/*
									 * Prevents errors during Hibernate
									 * validation. Hibernate calls HashCode()
									 * for no reason during em.persist() while
									 * doing the validation, causing problems
									 * when entities are created.
									 */
			return 0; // I guess that Hibernate validation does not really use
						// that value anyway. It's a hack.
		} else { // No id yet and not being persisted.

			return 0;
		}
	}

	@PrePersist
	public void prePersist() {
		isBeingPersisted = true;
	}

	@PostPersist
	public void postPersist() {
		isBeingPersisted = false;
	}


	@Override
	public String toString() {
		return this.getName();
	}

	
}
